#   Copyright 2006 Jerome Lacoste (jerome@coffeebreaks.org)
#
#   Licensed under the Apache License, Version 2.0 (the "License");
#   you may not use this file except in compliance with the License.
#   You may obtain a copy of the License at
#
#       http://www.apache.org/licenses/LICENSE-2.0
#
#   Unless required by applicable law or agreed to in writing, software
#   distributed under the License is distributed on an "AS IS" BASIS,
#   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
#   See the License for the specific language governing permissions and
#   limitations under the License.
# Methods added to this helper will be available to all templates in the application.
module ApplicationHelper

  def current_user_id
    session[:user_id]
  end

  def current_user
    User.find(session[:user_id])
  end

  def is_user_logged_in
    session[:user_id] != nil
  end

  def can_manage_users
    current_user_id == 1
  end

  #
  # Creates a link to the specified +action+, using the predefined image
  def link_to_destroy(obj, obj_name = obj.class.to_s.downcase, action = 'destroy')
    link_to image_tag("/images/cross.png", {:alt => 'delete this ' + obj_name, :size => "16x16", :border => 0, :class=>"paddedImage"}), { :action => action, :id => obj }, :confirm => 'Are you sure?', :method => "post"
  end

  def fmt_date(date)
    date.strftime("%a %d.%m.%Y")
  end

  def fmt_time(date)
    date.strftime("%H:%M")
  end

  def timezone_info
    "(all dates and times are in timezone '" + current_timezone.to_s + "')"
  end

  def current_timezone
    tz = ActiveRecord::Base.default_timezone
    tz = current_user.tzget if is_user_logged_in
    tz
  end

  def fmt_user_time(datetime)
    return datetime if !datetime.respond_to?(:strftime)
    datetime = current_user.tzget.utc_to_local(datetime) if is_user_logged_in
    datetime.strftime("%H:%M")
    # %I:%M %p
  end


  def fmt_duration(d)
    i = d.to_i
#    sprintf("%ih %i", i, (d - i) * 60)

    if (i == d)
      y = sprintf("%ih", i)
    else
      y = sprintf("%ih %i", i, (d - i) * 60)
    end
    return y
  end

  def fmt_assignees(assignees)
    assignees.collect() { |u| u.short }.join(", ")
  end

  def fmt_description(s, issue_tracker)
    return s if issue_tracker.nil?
    issue_tracker.to_html(s)
  end

  # stops at the first '\n' or +max_length+, whatever comes first
  # adds trails if truncated
  # the returned string length is maximum max_length
  def shorten_description(description, trails = "...", max_length = 80)
    description = strip_last_eol(description) # FIXME no trim ?
    eol = description.index("\n")
    description = eol.nil? ? description : (description[0, eol] + trails)

    if (description.length > max_length)
      displayed_trails = trails.nil? ? "" : trails[0, [trails.length, description.length, max_length].min]
      displayed_description_length = [0, [description.length, max_length].min - displayed_trails.length].max
      description = description[0, displayed_description_length] + displayed_trails
    end
    description
  end

  def strip_last_eol(s)
    return nil if s.nil?
    return "" if s == "\n"
    return s[0, s.length - 2] if (s[s.length - 1] == '\n')
    s
  end

end
